package com.javasm.service.goodsService.impl;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.javasm.common.page.PageResult;
import com.javasm.domin.criteria.goodsCriteria.GdTypeCriteria;
import com.javasm.domin.entity.goodsEntity.GdProduct;
import com.javasm.domin.entity.goodsEntity.GdType;
import com.javasm.domin.vo.goodsVo.GdTypeVo;
import com.javasm.mapper.goodsMapper.GdProductMapper;
import com.javasm.mapper.goodsMapper.GdTypeMapper;
import com.javasm.service.goodsService.GdTypeService;
import com.javasm.transfer.goodsTransfer.GdTypeTransfer;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import java.util.List;
import java.util.stream.Collectors;

/**
* @author Administrator
* @description 针对表【gd_type】的数据库操作Service实现
* @createDate 2021-11-19 11:19:10
*/
@Service
@Transactional
@RequiredArgsConstructor
public class GdTypeServiceImpl extends ServiceImpl<GdTypeMapper, GdType>
implements GdTypeService {

    @Autowired
    private GdTypeMapper gdTypeMapper;

    @Autowired
    private GdProductMapper gdProductMapper;


    @Autowired
    private GdTypeTransfer gdTypeTransfer;

    @Override
    public PageResult<GdTypeVo> selectCriteria(GdTypeCriteria gdTypeCriteria) {
        String typeName = gdTypeCriteria.getTypeName();

        LambdaQueryWrapper<GdType> lambda = new QueryWrapper<GdType>().lambda();

        if (!StringUtils.isEmpty(typeName)) {
            lambda.like(GdType::getTypeName, typeName);
        }

        PageHelper.startPage(gdTypeCriteria.getCurrentPage(), gdTypeCriteria.getPageSize());

        List<GdType> gdTypeList = gdTypeMapper.selectList(lambda);

        PageInfo<GdType> gdTypePageInfo = new PageInfo<>(gdTypeList);

        long total = gdTypePageInfo.getTotal();

        List<GdTypeVo> gdTypeVos = gdTypeTransfer.toVO(gdTypeList);


        return new PageResult<GdTypeVo>(total, gdTypeVos);
    }

    @Override
    public List<GdTypeVo> selectByPid() {
        LambdaQueryWrapper<GdType> lambda = new QueryWrapper<GdType>().lambda();
        lambda.eq(GdType::getPid, 0);
        List<GdType> gdTypes = gdTypeMapper.selectList(lambda);

        List<GdTypeVo> gdTypeVos = gdTypeTransfer.toVO(gdTypes);

        return gdTypeVos;
    }

    @Override
    public int batchDeleteByIds(List<Long> ids) {
        List<Long> iii = null;

        int i1 =0;

        for (int i = 0; i < ids.size(); i++) {

            Long aLong = ids.get(i);
            i1+=selectPid(aLong);


        }
        if (i1>0){
            return gdTypeMapper.deleteBatchIds(iii);
        }else {
            return gdTypeMapper.deleteBatchIds(ids);
        }

    }

    @Override
    public List<GdTypeVo> getAllTypeTree() {

        List<GdType> list = this.list();

        List<GdType> root = list.stream().filter(item -> item.getPid() == 0).collect(Collectors.toList());


        list.removeAll(root);

        List<GdTypeVo> gdTypeVosList = gdTypeTransfer.toVO(list);
        List<GdTypeVo> rootVo = gdTypeTransfer.toVO(root);

        for (int i = 0; i < root.size(); i++) {
            String typeName = root.get(i).getTypeName();
            Integer id = root.get(i).getId();
            rootVo.get(i).setValue(id);
            rootVo.get(i).setLabel(typeName);

        }
        rootVo.forEach(item ->getChildren(item, gdTypeVosList));


        return rootVo;
    }

    @Override
    public int selectPid(Long id) {
        GdType gdType = gdTypeMapper.selectById(id);
        if (gdType.getPid()==0){
            return 1;
        }
        List<GdType> list = gdTypeMapper.selectPid(id);

        if (list.size()>0) {
            return 1;
        }else {
            List<GdProduct> gdProducts = gdProductMapper.selectTypeIdGdProducts(id);

            if (gdProducts.size()>0){
                return 1;
            }else {
               return 0;
            }
        }
    }

    /**
     * 找孩子
     */
    public void getChildren(GdTypeVo typeVo, List<GdTypeVo> list) {
        List<GdTypeVo> second = list.stream().filter(item -> item.getPid().longValue() == typeVo.getId().longValue()).collect(Collectors.toList());
        if (second != null && second.size() > 0) {
            typeVo.setChildren(second);
            list.removeAll(second);
            for (int i = 0; i < second.size(); i++) {
                String typeName = second.get(i).getTypeName();
                Integer id = second.get(i).getId();
                List<GdTypeVo> children = typeVo.getChildren();
                children.get(i).setLabel(typeName);
                children.get(i).setValue(id);
                getChildren(children.get(i), list);
            }

        }
    }




    //新增下拉框选择信息
    @Override
    public List<GdTypeVo> getSelectTree() {
        //一级
        List<GdType> first = gdTypeMapper.getByCategoryLevel(1);
        List<GdTypeVo> typeVos = gdTypeTransfer.toVO(first);
        //二级
        List<GdType> second = gdTypeMapper.getByCategoryLevel(2);
        List<GdTypeVo> typeVos1 = gdTypeTransfer.toVO(second);
        typeVos.forEach(typeVo -> {
            List<GdTypeVo> collect = typeVos1.stream().filter(item -> item.getPid().longValue() == typeVo.getId().longValue()).collect(Collectors.toList());
            typeVo.setChildren(collect);
        });
        return typeVos;
    }

    @Override
    public int updateType(Long id) {

        int a =0;

        GdType gdType = gdTypeMapper.selectById(id);

        List<GdType> list = gdTypeMapper.selectList();

        for (int i = 0; i < list.size(); i++) {

            GdType gdType1 = list.get(i);
            if (gdType.getId()==gdType1.getPid()){
                a=1;
            }if (gdType.getPid()==0){
                a=1;
            }else{
                a=0;
            }


        }

        return a;
    }


}